{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Querying Data Tutorial\n",
    "\n",
    "MLDB comes with a powerful [SQL-like Select Query](../../../../doc/#builtin/sql/Sql.md.html) implementation accessible via its [REST API](../../../../doc/#builtin/sql/QueryAPI.md.html). This tutorial will show a few different ways to query data.\n",
    "\n",
    "The notebook cells below use `pymldb`; you can check out the [Using `pymldb` Tutorial](../../../../doc/nblink.html#_tutorials/Using pymldb Tutorial) for more details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pymldb import Connection\n",
    "mldb = Connection()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating a sample dataset\n",
    "\n",
    "First we will create a sample dataset, much like in the [Loading Data Tutorial](../../../../doc/nblink.html#_tutorials/Loading Data Tutorial):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>POST http://localhost/v1/datasets/example/commit</strong><br /><strong style=\"color: green;\">200 OK</strong><br /> "
      ],
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex = mldb.put('/v1/datasets/example', {\"type\":\"sparse.mutable\"})\n",
    "mldb.post('/v1/datasets/example/rows', { \"rowName\": \"r1\", \"columns\": [ [\"a\", 1, 0], [\"b\", 2, 0] ] })\n",
    "mldb.post('/v1/datasets/example/rows', { \"rowName\": \"r2\", \"columns\": [ [\"a\", 3, 0], [\"b\", 4, 0] ] })\n",
    "mldb.post('/v1/datasets/example/rows', { \"rowName\": \"r3\", \"columns\": [ [\"a\", 5, 0], [\"b\", 6, 0] ] })\n",
    "mldb.post('/v1/datasets/example/rows', { \"rowName\": \"r4\", \"columns\": [ [\"a\", 7, 0], [\"b\", 8, 0] ] })\n",
    "mldb.post('/v1/datasets/example/commit')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying into a DataFrame\n",
    "\n",
    "We can use the `query()` shortcut function to run queries and get the results as Pandas DataFrames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>_rowName</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>r4</th>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>r3</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>r2</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>r1</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a  b\n",
       "_rowName      \n",
       "r4        7  8\n",
       "r3        5  6\n",
       "r2        3  4\n",
       "r1        1  2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = mldb.query(\"select * from example\")\n",
    "print type(df)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying via REST\n",
    "\n",
    "We can also make lower-level REST API calls to the query endpoint in the [Query API](../../../../doc/#builtin/sql/QueryAPI.md.html), `/v1/query` for full SQL queries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>GET http://localhost/v1/query?q=select+%2A+from+example+where+a+%3E+4&format=table</strong><br /><strong style=\"color: green;\">200 OK</strong><br /> <div class=\"highlight\"><pre style=\"line-height: 125%\"><span></span>[\n",
       "  [\n",
       "    <span style=\"color: #0000dd\">&quot;_rowName&quot;</span>, \n",
       "    <span style=\"color: #0000dd\">&quot;a&quot;</span>, \n",
       "    <span style=\"color: #0000dd\">&quot;b&quot;</span>\n",
       "  ], \n",
       "  [\n",
       "    <span style=\"color: #0000dd\">&quot;r4&quot;</span>, \n",
       "    <span style=\"color: #0000dd\">7</span>, \n",
       "    <span style=\"color: #0000dd\">8</span>\n",
       "  ], \n",
       "  [\n",
       "    <span style=\"color: #0000dd\">&quot;r3&quot;</span>, \n",
       "    <span style=\"color: #0000dd\">5</span>, \n",
       "    <span style=\"color: #0000dd\">6</span>\n",
       "  ]\n",
       "]\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mldb.get('/v1/query', q=\"select * from example where a > 4\", format=\"table\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can control the format of the output JSON using the `format` attribute:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>GET http://localhost/v1/query?q=select+%2A+from+example+where+a+%3E+4&format=aos</strong><br /><strong style=\"color: green;\">200 OK</strong><br /> <div class=\"highlight\"><pre style=\"line-height: 125%\"><span></span>[\n",
       "  {\n",
       "    <span style=\"color: #333333; font-weight: bold\">&quot;_rowName&quot;</span>: <span style=\"color: #0000dd\">&quot;r4&quot;</span>, \n",
       "    <span style=\"color: #333333; font-weight: bold\">&quot;a&quot;</span>: <span style=\"color: #0000dd\">7</span>, \n",
       "    <span style=\"color: #333333; font-weight: bold\">&quot;b&quot;</span>: <span style=\"color: #0000dd\">8</span>\n",
       "  }, \n",
       "  {\n",
       "    <span style=\"color: #333333; font-weight: bold\">&quot;_rowName&quot;</span>: <span style=\"color: #0000dd\">&quot;r3&quot;</span>, \n",
       "    <span style=\"color: #333333; font-weight: bold\">&quot;a&quot;</span>: <span style=\"color: #0000dd\">5</span>, \n",
       "    <span style=\"color: #333333; font-weight: bold\">&quot;b&quot;</span>: <span style=\"color: #0000dd\">6</span>\n",
       "  }\n",
       "]\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mldb.get('/v1/query', q=\"select * from example where a > 4\", format=\"aos\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Where to next?\n",
    "\n",
    "Check out the other [Tutorials and Demos](../../../../doc/#builtin/Demos.md.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
